package com.xunua.NaoZiKongBai.MainActivity;

import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.snackbar.Snackbar;
import com.trello.rxlifecycle2.components.support.RxFragment;
import com.xunua.NaoZiKongBai.Base.BaseActivity;
import com.xunua.NaoZiKongBai.Bean.Bmob.MyUser;
import com.xunua.NaoZiKongBai.Bean.EventBus.UpdatePwdEvent;
import com.xunua.NaoZiKongBai.Bean.EventBus.WoDeRefreshEvent;
import com.xunua.NaoZiKongBai.Fragments.ShiPinFragment;
import com.xunua.NaoZiKongBai.Fragments.WoDeFragment;
import com.xunua.NaoZiKongBai.Fragments.ShouYe.ShouYeFragment;
import com.xunua.NaoZiKongBai.R;
import com.xunua.NaoZiKongBai.View.MyJzvdStd;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FetchUserInfoListener;
import cn.bmob.v3.listener.UpdateListener;


public class MainActivity extends BaseActivity<MainPresenter, MainContract.VP> implements View.OnClickListener {

    @BindView(R.id.main_statusBar)
    View mainStatusBar;
    @BindView(R.id.main_fragment)
    FrameLayout mainFragment;
    @BindView(R.id.main_rb_shouye)
    RadioButton mainRbShouye;
    @BindView(R.id.main_rb_shipin)
    RadioButton mainRbShipin;
    @BindView(R.id.main_rb_wode)
    RadioButton mainRbWode;
    @BindView(R.id.main_rg)
    RadioGroup mainRg;

    private ShouYeFragment shouYeFragment;
    private ShiPinFragment shiPinFragment;
    private WoDeFragment woDeFragment;
    private List<RxFragment> fragments;
    private FragmentTransaction fragmentTransaction;

    @Override
    public MainContract.VP getContract() {
        return new MainContract.VP() {
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setContentViewId() {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    public void init() {
        initFragment();
        initRadioGroup();
    }

    //设置导航栏点击事件监听
    private void initRadioGroup() {
        mainRbShouye.setOnClickListener(this);
        mainRbShipin.setOnClickListener(this);
        mainRbWode.setOnClickListener(this);
        mainRbShouye.performClick();
    }

    private void initFragment() {
        shouYeFragment = new ShouYeFragment();
        shiPinFragment = new ShiPinFragment();
        woDeFragment = new WoDeFragment();
        fragments =new ArrayList<>();
        fragments.add(shouYeFragment);
        fragments.add(shiPinFragment);
        fragments.add(woDeFragment);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Fragment f :
                fragments) {
            fragmentTransaction.add(R.id.main_fragment,f);
            fragmentTransaction.hide(f);
        }
        fragmentTransaction.commit();
    }

    @Override
    public MainPresenter getPresenterInstance() {
        return new MainPresenter();
    }

    @Override
    public boolean IskeyboardEnable() {
        return false;
    }

    @Override
    public View getStatusBar() {
        return mainStatusBar;
    }

    @Override
    public void onClick(View v) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (v.getId()){
            case R.id.main_rb_shouye:
                if (!shouYeFragment.isHidden())
                EventBus.getDefault().post("刷新新闻");
                for (int i = 0; i < fragments.size(); i++) {
                    if (i == 0) {
                        fragmentTransaction.show(fragments.get(i));
                    }else {
                        fragmentTransaction.hide(fragments.get(i));
                    }
                }
                fragmentTransaction.commit();
                break;
            case R.id.main_rb_shipin:
                for (int i = 0; i < fragments.size(); i++) {
                    if (i == 1) {
                        fragmentTransaction.show(fragments.get(i));
                    }else {
                        fragmentTransaction.hide(fragments.get(i));
                    }
                }
                fragmentTransaction.commit();
                break;
            case R.id.main_rb_wode:
                if (!woDeFragment.isHidden())
                    EventBus.getDefault().post(new WoDeRefreshEvent(true));//提示刷新

                for (int i = 0; i < fragments.size(); i++) {
                    if (i == 2) {
                        fragmentTransaction.show(fragments.get(i));
                    }else {
                        fragmentTransaction.hide(fragments.get(i));
                    }
                }
                fragmentTransaction.commit();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (MyJzvdStd.backPress()){
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyJzvdStd.releaseAllVideos();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK&&!MyJzvdStd.backPress()){
//            Toast.makeText(myApplication, "不让你走", Toast.LENGTH_SHORT).show();
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updatePwdEvent(UpdatePwdEvent event){
        showLoadingDialogBase(this,"正在修改",false);
        updateUser(mainRg,event);
    }
    /**
     * 更新用户操作并同步更新本地的用户信息
     */
    private void updateUser(final View view,UpdatePwdEvent event) {
        BmobUser.fetchUserInfo(new FetchUserInfoListener<BmobUser>() {
            @Override
            public void done(BmobUser user, BmobException e) {
                if (e == null) {
                    MyUser myuser = BmobUser.getCurrentUser(MyUser.class);
                    if (myuser.getPwd_look().equals(event.getOldpwd())) {
                        myuser.setPassword(event.getNewpwd());
                        myuser.setPwd_look(event.getNewpwd());
                        myuser.update(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null||e.getErrorCode()==9015) {
                                    //修改成功
                                    hideLoadingDialogBase();
                                    Snackbar.make(view, "修改密码成功,您的新密码为"+myuser.getPwd_look(), Snackbar.LENGTH_LONG).show();
                                }else {
                                    //修改失败
                                    hideLoadingDialogBase();
                                    Snackbar.make(view, "修改密码失败" + e.getErrorCode(), Snackbar.LENGTH_LONG).show();
                                }
                            }
                        });
                    }else {
                        //修改失败
                        hideLoadingDialogBase();
                        Snackbar.make(view, "修改密码失败：原始密码输入错误", Snackbar.LENGTH_INDEFINITE).show();
                    }
                }else {
                    //信息同步失败
                    hideLoadingDialogBase();
                    Snackbar.make(view, "更新用户信息失败：" + e.getMessage(), Snackbar.LENGTH_INDEFINITE).show();
                }
            }
        });
    }
}
